package org.msfj.common.handler;

import org.msfj.common.enums.HttpCode;
import org.msfj.common.exception.BaseException;
import org.msfj.common.model.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by ace on 2017/9/8.
 */
@ControllerAdvice("org.msfj")
@ResponseBody
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(BaseException.class)
    public CommonResult baseExceptionHandler(HttpServletResponse response, BaseException ex) {
        logger.error(ex.getMessage(), ex);
        return CommonResult.build(ex.getStatus(), ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public CommonResult otherExceptionHandler(HttpServletResponse response, Exception ex) {
        logger.error(ex.getMessage(), ex);
        return CommonResult.build(HttpCode.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
    }
}
